package controller;

import model.Figure;
import model.Monster;
import model.Player;

import java.io.IOException;
import java.util.Random;

public class FightController {
    static Random rand = new Random();
    public static void attack(Player attacker, Monster defender) throws IOException {
        if(rand.nextInt(attacker.getDex())>=rand.nextInt(defender.getDex())) {
            int damage = calDamage(attacker, defender);
            if(damage<10){
                damage = 10;
            }
            defender.setHp(defender.getHp()-damage);
            MessageController.showToPlayer(attacker,"您造成了"+damage+"伤害，敌人Hp:"+defender.getHp()+"/"+defender.getMax_hp());
        }else {
            MessageController.showToPlayer(attacker,"您的攻击被躲开了。");
        }
        if(rand.nextInt(attacker.getDex())<=rand.nextInt(defender.getDex())) {
            int damage = calDamage(defender,attacker);
            if(damage<10){
                damage = 10;
            }
            attacker.setHp(attacker.getHp()-damage);
            MessageController.showToPlayer(attacker,"您受到了"+damage+"伤害，Hp:"+attacker.getHp()+"/"+attacker.getMax_hp());
        }else {
            MessageController.showToPlayer(attacker,"您躲开了敌人的反击。");
        }
        if(defender.getHp()<=0) {
            MessageController.showToPlayer(attacker,"您击杀了敌人。");
            RoomController.getRoom(defender.getLocation()).mosterRemove(defender.getName());
        }
        if(attacker.getHp()<=0) {
            MessageController.showToPlayer(attacker,"您被击杀了。");
            attacker.setHp(attacker.getMax_hp());
            attacker.setLocation("zhucheng");
            PlayerController.initPlayer(attacker);
        }
    }
    public static void attack(Player attacker, Player defender) throws IOException {
        if(rand.nextInt(attacker.getDex()/10+1)>=rand.nextInt(defender.getDex()/10)) {
            int damage = calDamage(attacker,defender);
            if(damage<10){
                damage = 10;
            }
            defender.setHp(defender.getHp()-damage);
            MessageController.showToPlayer(attacker,"您造成了"+damage+"伤害，敌人Hp:"+defender.getHp()+"/"+defender.getMax_hp());
            MessageController.showToPlayer(defender,"您受到了"+damage+"伤害，Hp:"+defender.getHp()+"/"+defender.getMax_hp());
        }else {
            MessageController.showToPlayer(attacker,"您的攻击被躲开了。");
            MessageController.showToPlayer(defender,"您躲开了来自"+attacker.getName()+"的攻击。");
        }
        if(rand.nextInt(attacker.getDex()/10)<=rand.nextInt(defender.getDex()/10+1)) {
            int damage = calDamage(defender,attacker);
            if(damage<10){
                damage = 10;
            }
            attacker.setHp(attacker.getHp()-damage);
            MessageController.showToPlayer(attacker,"您受到了"+damage+"伤害，Hp:"+attacker.getHp()+"/"+attacker.getMax_hp());
            MessageController.showToPlayer(defender,"您造成了"+damage+"伤害，敌人Hp:"+attacker.getHp()+"/"+attacker.getMax_hp());
        }else {
            MessageController.showToPlayer(defender,"您的攻击被躲开了。");
            MessageController.showToPlayer(attacker,"您躲开了敌人的反击");
        }
        if(defender.getHp()<=0) {
            MessageController.showToPlayer(attacker,"您击杀了敌人。");
            MessageController.showToPlayer(defender,"您被击杀了。");
            defender.setHp(defender.getMax_hp());
            defender.setLocation("zhucheng");
            PlayerController.initPlayer(defender);
        }
        if(attacker.getHp()<=0) {
            MessageController.showToPlayer(defender,"您击杀了敌人。");
            MessageController.showToPlayer(attacker,"您被击杀了。");
            attacker.setHp(attacker.getMax_hp());
            attacker.setLocation("zhucheng");
            PlayerController.initPlayer(attacker);
        }
    }
    public static int calDamage(Figure attacker, Figure defender) {
        return (int) (attacker.getStr()*0.7+attacker.getCon()*0.3-defender.getStr()*0.3-defender.getCon()*0.2);
    }
}
